/*
 * @Author: 郭桓
 * @Date: 2019-02-19 10:29:14
 * @LastEditors: 郭桓
 * @LastEditTime: 2019-03-04 11:13:57
 * @Description: file content
 */
// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        speed: 80,
        _noticeList: null,
        _noticeIndex: -1,
        _startX: 0,
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this._noticeList = [];
        this._noticeIndex = -1;
        this._startX = this.node.x;
    },

    start() {

    },

    /**
     * @description:  设置广播
     * @param {type} 
     * @return: 
     */
    startLoopNotce(str) {
        if (typeof str == 'string') {
            this._noticeList.push(str);
        } else {
            this._noticeList = this._noticeList.concat(str);
        }
    },


    startLoop() {
        this._noticeIndex++;
        if (this._noticeIndex >= this._noticeList.length) {
            this._noticeIndex = 0;
        }
        let infoStr = this._noticeList[this._noticeIndex % this._noticeList.length];
        let width = this.node.getChildByName('words1').width;
        this.node.getChildByName('name').getComponent(cc.Label).string = infoStr.name + '连闯三关,';
        width += this.node.getChildByName('name').width;
        this.node.getChildByName('info').getComponent(cc.Label).string = '获得' + infoStr.good + "一支";
        this.node.getChildByName('state').getComponent(cc.Label).string = infoStr.state == null ? '' : ',' + infoStr.state;



        width += this.node.getChildByName('info').width;
        width += 530;

        this.node.x = this._startX;


        let dutime = width / this.speed;
        // this.node.runAction(cc.moveTo(dutime,cc.v2(-width,0)));
        this.node.runAction(cc.sequence(cc.moveTo(dutime, cc.v2(-width, 0)), cc.callFunc(this.startLoop.bind(this), this)));




    }

    // update (dt) {},
});